package win.wellcoding.wechat.apis.accessToken;

import org.apache.struts2.ServletActionContext;
import org.json.JSONObject;
import org.junit.Test;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import win.wellcoding.wechat.apis.utils.HttpUtils;

public class ReceiveAccessToken {

	@Test
	public void received(){
		WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());
        RedisTemplate<String, Object> redisTemplate = (RedisTemplate) applicationContext.getBean("redisTemplate");

		String token = null;
		String accessToken = HttpUtils.sendGet("https://api.weixin.qq.com/cgi-bin/token", "grant_type=client_credential&appid=wxe4fb6a8712bbefb3&secret=ff1b913072d2178a927e11eaec11a7e0");
		JSONObject jsonObject = new JSONObject(accessToken);
		if (jsonObject.has("errcode")){
			
		}else{
			token = jsonObject.getString("access_token");
			redisTemplate.opsForHash().put("parameters", "access_token",token);
			System.out.println(token);
		}
	}
}
